package main

import (
	"context"
	"fmt"
	"go.mongodb.org/mongo-driver/bson"
	"go.mongodb.org/mongo-driver/mongo"
	"log"
	"strconv"
	"time"

	"webFull/config"
	cartInfo "webFull/internal/model/proto/cartInfo/gen"
	gameObj "webFull/internal/model/proto/game/gen"
	userInfo "webFull/internal/model/proto/userInfo/gen"
	"webFull/internal/service"
)

/*
bson.D, internally is primitive.D, which is []primitive.E, which is a struct.
bson.M, internally is primitive.M, which is map[string]interface{}. You put in key/value in bson.M but use document (struct) in bson.D.
It is better to explain it using 2 parameters, e.g. search for a = 1 and b = 2. You syntax will be: bson.M{"a": 1, "b": 2} or bson.D{{"a": 1}, {"b": 2}}
*/

func SuccessPay(appIds ...int32) {
	newGames := entity.NewSet[int32, string](appIds...)
	// 已有游戏
	userId, b2 := service.FindByUserId[userInfo.UserDetail](db.MongoColl[db.UserDetail], 55291428)
	var needAdd entity.Set[int32, string]
	if b2 {
		set := entity.MapKeyToSet(userId.WishList)
		needAdd = newGames.DiffSet(set)
	}
	fmt.Println(needAdd == nil)

}
func main() {
	ctx := context.Background()
	categories, _ := service.FindGameTags(ctx, db.MongoColl[db.GameInfos], []string{"1"})
	fmt.Println(categories)

}

func asd() {
	//db.userinfo.find(
	//{
	//arrTwo:
	//	{
	//		$elemMatch:
	//		{
	//		id:
	//			1
	//		}
	//	}
	//})
	mongoUtils.Handle(context.Background(), db.MongoColl[db.GameInfos], func(ctx context.Context, coll *mongo.Collection) bool {
		//filter := bson.M{
		//	"categories": bson.M{"$elemMatch": bson.M{"id": 13}}}
		das := []int32{1, 20, 49, 36, 9, 38, 22, 29}

		filter := bson.M{
			"categories.id": bson.M{"$all": das}}

		var results []*gameObj.GameObj
		cursor, err := coll.Find(ctx, filter)
		if err != nil {
			return false
		}

		defer cursor.Close(ctx)
		if err = cursor.All(ctx, &results); err != nil {
			log.Fatal(err)
		}

		for _, result := range results {

			fmt.Println(result.Categories)

		}
		return false
	})
}

func TT() {
	if orderCart, ok := service.CreateOrderCart(55291428, cartInfo.OrderCartState_OrderCart_WAIT, 304390, 563560); ok {
		redisUtils.Set[string](db.RedisRdb, db.OrderCart+":"+strconv.FormatInt(int64(55291428), 10), orderCart.OrderId, 10*time.Minute, false)
		cart := service.UpdateOrderCart(orderCart.OrderId, func(cart *cartInfo.OrderCart) *cartInfo.OrderCart {
			return orderCart
		})
		fmt.Println(cart)

		service.UpdateOrderCart(orderCart.OrderId, func(cart *cartInfo.OrderCart) *cartInfo.OrderCart {
			cart.OrderState = cartInfo.OrderCartState_OrderCart_FINISH
			cart.PayType = "Wechat"
			return cart
		})
	}
}
